/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.article;

import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import model.generic.TableModelWithListeners;
import util.article.EancodeDetails;
import util.article.ModelDetails;
import util.article.SizeDetails;

/**
 *
 * @author Dirk
 */
public class EancodeTableModel extends TableModelWithListeners {
    
    private ArrayList<EancodeDetails> eancodes;
    private ArrayList<JComboBox<ModelDetails>> modelCmbs;
    private final String[] columns = {"Model", "Maat", "Eancode"};
    private final String nullObject = "Algemeen";
    
    private ModelTableModel modelModel;
    
    
    public EancodeTableModel(ModelTableModel modelModel) {
        this.modelModel = modelModel;
        eancodes = new ArrayList<EancodeDetails>();
        modelCmbs = new ArrayList<JComboBox<ModelDetails>>();
        JComboBox<ModelDetails> cmb = new JComboBox<ModelDetails>(modelModel.getAllModels().toArray(new ModelDetails[0]));
        modelCmbs.add(cmb);
    }
    
    
    
    @Override
    public int getRowCount() {
        return eancodes.size() + 1;
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(rowIndex < eancodes.size()){
            if(columnIndex == 0){
                ModelDetails model = eancodes.get(rowIndex).getModelid();
                return model != null ? model.getCode() : "Selecteer een model";
            }
            else if(columnIndex == 1){
                SizeDetails size = eancodes.get(rowIndex).getSizeid();
                return size != null ? size.getName() : "Selecteer een maat";
            }
            else{
                int eancode = eancodes.get(rowIndex).getEancode();
                return eancode > -1 ? eancode : "";
            }
        } else{
            if(columnIndex == 0)
                return "Selecteer een model";
            else if(columnIndex == 1)
                return "Selecteer een maat";
            else
                return "";
        }
            
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if(rowIndex >= eancodes.size()){
            EancodeDetails size = new EancodeDetails(-1, -1, null, null);
            eancodes.add(size);}
        if(columnIndex == 0)
            eancodes.get(rowIndex).setModelid((ModelDetails)aValue);
        else if(columnIndex == 1)
            eancodes.get(rowIndex).setSizeid((SizeDetails)aValue);
        else{
            try{
                eancodes.get(rowIndex).setEancode(Integer.parseInt((String)aValue));
            } catch (NumberFormatException ex){
                eancodes.get(rowIndex).setEancode(-1);
                JOptionPane.showMessageDialog(null, "Gelieve een geldige eancode in te geven.", "Foute invoer", JOptionPane.ERROR_MESSAGE);
            }
        }
        this.fireTableDataChanged();
    }
    
    public void addEancodes(Collection<EancodeDetails> eancodesList){
        eancodes.addAll(eancodesList);
        this.fireTableDataChanged();
    }

    @Override
    public void removeItem(int index) {
        eancodes.remove(index);
        this.fireTableDataChanged();
    }

    
    
}
